package com.aidex.web.controller.biz;

import com.aidex.biz.domain.RubbishType;
import com.aidex.biz.domain.vo.RubbishTypeEls;
import com.aidex.biz.service.RubbishTypeService;
import com.aidex.common.annotation.Log;
import com.aidex.common.constant.Constants;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.TreeNode;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;

/**
 * 分类信息管理
 *
 * @author ruoyi
 */

@RestController
@RequestMapping("/biz/bizRubbishType")
public class RubbishTypeController extends BaseController {

    @Autowired
    private RubbishTypeService sysRubbishTypeService;

    /**
     * 根据层级展开部门树表格
     *
     * @param level 展开层级
     * @param id    起始展开ID
     * @return com.aidex.common.core.domain.R
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/list/{level}/{id}/{busType}")
    public R list(@PathVariable("level") @NotEmpty int level, @PathVariable("id") String id, @PathVariable(
            "busType") Integer busType) {
        if (level == 0) {
            level = 2;
        }
        RubbishType sysRubbishType = new RubbishType();
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            sysRubbishType.setCompanyId(null);
            id = Constants.TREE_ROOT;
        } else {
            sysRubbishType.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        sysRubbishType.setBusType(busType);
        List<RubbishType> depts = sysRubbishTypeService.listDataByLevel(sysRubbishType, level, id);
        return R.data(depts);
    }

    /**
     * 根据ID获取部门信息
     *
     * @param id
     * @return com.aidex.common.core.domain.R
     */
    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @GetMapping(value = "/{id}")
    public R detail(@NotBlank @PathVariable String id) {
        RubbishType sysRubbishType = sysRubbishTypeService.get(id);
        RubbishType parentRubbishType = sysRubbishTypeService.get(sysRubbishType.getParentId());
        if (parentRubbishType != null) {
            sysRubbishType.setParentName(parentRubbishType.getDeptName());
            sysRubbishType.setParentDeptType(parentRubbishType.getDeptType());
        }
        return R.data(sysRubbishType);
    }

    /**
     * 获取当前父节点下最大编号
     *
     * @param parentId
     * @return com.aidex.common.core.domain.R
     */
    // @PreAuthorize("@ss.hasPermi('system:post:query')")
    @Log(title = "岗位管理", businessType = BusinessType.SELECT)
    @GetMapping("/findMaxSort/{parentId}")
    public R findMaxSort(@PathVariable("parentId") String parentId) {

        RubbishType sysRubbishType = new RubbishType();
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            sysRubbishType.setCompanyId(null);
        } else {
            sysRubbishType.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }

        sysRubbishType.setParentId(parentId);
        return R.data(sysRubbishTypeService.findMaxSort(sysRubbishType));
    }

    /**
     * 校验同一个父部门下是否存在同名的部门
     *
     * @param deptName 部门名称
     * @param parentId 父部门ID
     * @param id
     * @return com.aidex.common.core.domain.R
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "部门管理", businessType = BusinessType.CHECK)
    @GetMapping(value = {"/validateRubbishTypeNameUnique/{deptName}/{parentId}/{id}", "/validateRubbishTypeNameUnique"
            + "/{deptName}/{parentId}" })
    public R validateRubbishTypeNameUnique(@NotBlank(message = "部门名称不允许为空") @PathVariable("deptName") String deptName
            , @NotBlank(message = "ID不允许为空") @PathVariable("parentId") String parentId, @PathVariable(value = "id",
            required = false) String id) {

        RubbishType sysRubbishType = new RubbishType();
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            sysRubbishType.setCompanyId(null);
        } else {
            sysRubbishType.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        sysRubbishType.setParentId(parentId);
        sysRubbishType.setDeptName(deptName);
        sysRubbishType.setId(id);
        sysRubbishTypeService.checkRubbishTypeNameUnique(sysRubbishType);
        return R.status(Boolean.TRUE);
    }

    /**
     * 新增部门
     *
     * @param dept
     * @return com.aidex.common.core.domain.R
     */
    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated RubbishType dept) {
        if (StringUtils.isEmpty(dept.getCompanyId())) {
            dept.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        dept.setStatus("0");
        sysRubbishTypeService.save(dept);
        return R.data(dept);
    }

    /**
     * 修改部门
     *
     * @param dept
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dept:edit')")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated RubbishType dept) {
        sysRubbishTypeService.save(dept);
        return R.data(dept);
    }

    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dept:remove')")
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable String id) {
        RubbishType sysRubbishType = new RubbishType();
        sysRubbishType.setId(id);
        return R.status(sysRubbishTypeService.remove(sysRubbishType));
    }

    /**
     * 部门选择树
     *
     * @param level 初始展开层级
     * @param id    展开节点ID
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/listTree/{level}/{id}/{busType}")
    public R listTree(@NotBlank @PathVariable("level") int level, @PathVariable("id") String id, @PathVariable(
            "busType") Integer busType) {
        if (level == 0) {
            level = 2;
        }
        RubbishType sysRubbishType = new RubbishType();
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            sysRubbishType.setCompanyId(null);
            id = Constants.TREE_ROOT;
        } else {
            sysRubbishType.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        sysRubbishType.setBusType(busType);
        List<TreeNode> depts = sysRubbishTypeService.buildRubbishTypeTree(sysRubbishType, level, id);
        return R.data(depts);
    }


    /**
     * 部门选择树
     *
     * @param level
     * @param id
     * @param excludeId 排除节点
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping(value = {"/listTreeExcludeChild/{level}/{id}/{excludeId}" })
    public R listTreeExcludeChild(@NotBlank @PathVariable("level") int level, @PathVariable("id") String id,
            @PathVariable(value = "excludeId", required = false) String excludeId) {
        if (level == 0) {
            level = 2;
        }

        id = SecurityUtils.getLoginUser().getUser().getSysDept().getParentId();//默认根据当前登陆对下的父节点
        RubbishType sysRubbishType = new RubbishType();
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            sysRubbishType.setCompanyId(null);
        } else {
            sysRubbishType.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<TreeNode> depts = sysRubbishTypeService.buildRubbishTypeTreeExcludeChild(sysRubbishType, 10, id,
                excludeId);
        return R.data(depts);
    }


    /**
     * 通用选人页面根据用户ID
     *
     * @return
     */
    @PostMapping(value = {"/getRubbishTypeInfoByIds" })
    public R getRubbishTypeInfoByIds(@Validated @RequestBody JSONObject deptIdsObj) {
        List<Map<String, Object>> sysRubbishTypeList = sysRubbishTypeService.getRubbishTypeInfoByIds(deptIdsObj);
        return R.data(sysRubbishTypeList);
    }


    /**
     * 按时间导入数据
     *
     * @return
     */
    @GetMapping("/importlist")
    public R importlist(RubbishType sysRubbishType) {
        sysRubbishType.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<RubbishTypeEls> rubbishTypeEls = sysRubbishTypeService.getRubbishTypexls(sysRubbishType);
        ExcelUtil<RubbishTypeEls> util = new ExcelUtil<RubbishTypeEls>(RubbishTypeEls.class);
        return util.exportExcel(rubbishTypeEls, StringUtils.getStringDate() + "回收数据");

    }

    /**
     * 首页柱状统计数据
     *
     * @return
     */
    @GetMapping("/getRubbishTypeStatement")
    public R getRubbishTypeStatement(RubbishType sysRubbishType) {
        sysRubbishType.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<Map<String, Object>> rubbishTypeStatement = sysRubbishTypeService.getRubbishTypeStatement(sysRubbishType);
        return R.data(rubbishTypeStatement);

    }

    /**
     * 获取所有分类信息
     * @param sysRubbishType
     * @return
     */
    @GetMapping("/getRubbishTypeList")
    public R<List<RubbishType>> getRubbishTypeList(RubbishType sysRubbishType) {
        return R.data(sysRubbishTypeService.findList(sysRubbishType));
    }



}
